package edu.ccut.saturn.workflow.start;

import java.util.HashMap;
import java.util.List;

import edu.ccut.saturn.component.ComponentFactory;
import edu.ccut.saturn.component.DefaultComponent;
import edu.ccut.saturn.component.IParameterContext;
import edu.ccut.saturn.workflow.common.Constants;
import edu.ccut.saturn.workflow.model.LogicKeyInfo;
import edu.ccut.saturn.workflow.model.ParameterInfo;
import edu.ccut.saturn.workflow.model.StartLimitInfo;
import edu.ccut.saturn.workflow.model.WorkFlowInfo;

public class IsOkforStart extends DefaultComponent {
	@SuppressWarnings({ "deprecation", "unchecked" })
	@Override
	public void execute(IParameterContext parameterContext) throws Exception {
		WorkFlowInfo workflowinfo= (WorkFlowInfo)parameterContext.getParameterValue(0);
		String process_define_id = (String ) parameterContext.getParameterValue(1);
		HashMap <String,String> formdatavalue = (HashMap<String,String>)parameterContext.getParameterValue(2);
		boolean isOkforStart = true;
		StartLimitInfo startLimit=workflowinfo.getStartInfo().getStartLimit();
		String startLimitType  = startLimit.getId();
		if("0".equals(startLimitType)){//直接启动
			
		}else 
		if("1".equals(startLimitType)){//规则启动
			 LogicKeyInfo startLimitlogic = startLimit.getLogicKeyInfo();
			 IParameterContext tempParameterContext = ComponentFactory// 上下文不能重用
				.createParameterContext();
			 String fullkey = startLimitlogic.getFullKey();//要启动的校验
			 List<ParameterInfo> paramList = startLimitlogic.getParamList();
			 int i =-1;
			 int out = 0;
			 for(ParameterInfo param:paramList){
				 i++;
				 String paramkey = param.getKey();
				 if(Constants.STARTLIMIT_EXTLOGIC_PARAMETER_PDI.equals(paramkey)){//如果是流程定义id
					 tempParameterContext.addParameter(process_define_id, String.class);
				 }else if(Constants.STARTLIMIT_EXTLOGIC_PARAMETER_IOS.equals(paramkey)){//如果是isOkforStart标识
					 tempParameterContext.addParameter(null, Boolean.class);
					 out= i;
				 }else{//非定制参数　根据key 到formdata中取值
					 tempParameterContext.addParameter(formdatavalue.get(paramkey), String.class);
				 }
			 }
			executeLogicSupportsTransaction(parameterContext, tempParameterContext, fullkey);
			isOkforStart = (Boolean)tempParameterContext.getParameterValue(out);
		}
		
		
		parameterContext.setParameter(3, isOkforStart);
	}
}